<template>
  <div class="noticleManage">
    <div class="btnRow">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="record">
      <el-table :data="tableList" style="width: 100%">
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-link type="primary" @click="edit(scope.row)">编辑</el-link>
            <el-link type="danger" @click="del(scope.row)">删除</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="noticeId" label="通知信息id"></el-table-column>
        <el-table-column prop="notice" label="通知信息"></el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        align="right"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="totalNumber"
        style="margin-top: 58px; margin-bottom: 40px"
      ></el-pagination>
    </div>
    <el-dialog
      :title="diaTitle"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form1" label-width="100px">
        <el-form-item label="通知栏id">
          <el-input v-model="noticeId" placeholder="通知栏id
          "></el-input>
        </el-form-item>
        <el-form-item label="通知栏内容">
          <el-input v-model="notice" placeholder="通知栏内容"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "noticleManage",
  data() {
    return {
      dialogVisible: false,
      pageIndex: 1,
      pageSize: 10,
      totalNumber: 0,
      diaTitle: "",
      tableList: [],
      notice: "",
      noticeId: ""
    };
  },
  created() {
    this.getNoticleList();
  },
  methods: {
    getNoticleList() {
      let params = {
        pageIndex: 1,
        pageSize: 10
      };
      this.$axios
        .get("/api/CjwNoticeList/GetNoticeList", { params })
        .then(res => {
          console.log(res);
          this.tableList = res.data.noticeLists;
          this.totalNumber = res.data.total;
        });
    },
    add() {
      this.diaTitle = "新增";
      this.notice = "";
      this.noticeId = "";
      this.dialogVisible = true;
    },
    edit(item) {
      this.diaTitle = "编辑";
      this.notice = item.notice;
      this.noticeId = item.noticeId;
      this.dialogVisible = true;
    },
    del(item) {
      this.$confirm("此操作将会删除选中的消息,是否确定?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(item.noticeId);
          this.$axios
            .get("/api/CjwNoticeList/DelNotice?noticeId=" + item.noticeId)
            .then(res => {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.getNoticleList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    formConfirm() {
      if (this.diaTitle == "新增") {
        let params = {
          noticeId: Number(this.noticeId),
          notice: this.notice
        };
        console.log(params);
        this.$axios.post("/api/CjwNoticeList/AddNotice", params).then(res => {
          this.$message({
            type: "success",
            message: "新增成功"
          });
          this.dialogVisible = false;
          this.getNoticleList();
        });
      } else {
        let params = {
          noticeId: Number(this.noticeId),
          notice: this.notice
        };
        this.$axios.post("/api/CjwNoticeList/EditNotice", params).then(res => {
          this.$message({
            type: "success",
            message: "编辑成功"
          });
          this.dialogVisible = false;
          this.getNoticleList();
        });
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getNoticleList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getNoticleList();
    }
  }
};
</script>

<style scoped>
.btnRow {
  margin: 10px;
}
</style>